<!--
 * @Author: awada 10254500+awadaawada@user.noreply.gitee.com
 * @Date: 2022-10-12 14:30:47
 * @LastEditors: awada 10254500+awadaawada@user.noreply.gitee.com
 * @LastEditTime: 2022-10-15 14:04:12
 * @FilePath: \JavaScriptd:\Briup\实训三\项目\第三个项目\点单项目\HeyTea_project\HeyTea-ui\src\pages\admin\category\List.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <!-- 产品栏目管理 -->
  <div>
    <!-- 新增 -->
    <el-button type="primary" @click="toAdd()">新增</el-button>
    <br>
    <!-- 表格 -->
    <el-table
      :data="res1"

      style="width: 90%;margin: 10px auto;"
    >
      <el-table-column
        type="index"
        :index="1"
        label="序号"
        width="180"
        align="center"
      />
      <el-table-column
        prop="id"
        label="栏目编号"
        width="180"
        align="center"
      />
      <el-table-column
        prop="name"
        label="名称"
        align="center"
      />
      <el-table-column
        align="center"
        label="图标"
      >
        <template v-slot="scope">
          <img width="20px" height="20px" :src="scope.row.icon" alt="加载失败">
        </template>
      </el-table-column>
      <el-table-column
        prop="num"
        label="排序号"
        align="center"
      />
      <el-table-column

        label="操作"
        width="100"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="toEdit(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="handleClick(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="visible" @close="resetForm">
      <el-form ref="myForm" :model="form" :rules="rules">
        <el-form-item
          label="产品名称"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item
          label="所属栏目"
          :label-width="formLabelWidth"
          prop="productCategoryId"
        >
          <el-select
            v-model="form.productCategoryId"
            placeholder="请选择所属栏目"
          >
            <el-option
              v-for="item in columns"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="活动名称"
          :label-width="formLabelWidth"
          prop="price"
        >
          <el-input v-model="form.price" autocomplete="off" />
        </el-form-item>
        <el-form-item label="封面" :label-width="formLabelWidth" prop="photo">
          <el-upload
            class="avatar-uploader"
            :action="uploadFileURL"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="form.photo" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="产品描述" :label-width="formLabelWidth">
          <el-input v-model="form.description" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="toSubmit">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { get } from '@/utils/request'
import { uploadFileURL } from '@/utils/config'
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10
      },
      form: {},
      loading: false,
      uploadFileURL: '',
      total: 0,
      title: '',
      res1: [],
      visible: false,
      rules: {
        name: [{ required: true, message: '请输入产品名称', trigger: 'blur' }],
        productCategoryId: [
          { required: true, message: '请选择所属栏目', trigger: 'change' }
        ],
        price: [{ required: true, message: '请输入价格', trigger: 'blur' }],
        photo: [{ required: true, message: '请上传封面', trigger: 'change' }]
      }
    }
  },
  computed: {},
  created() {
    this.getLanmu()
    this.toSubmit()
    this.toAdd()
    this.toEdit()
    this.handleClick()
  },
  mounted() {},
  methods: {
    async getLanmu() {
      const res = await get('/productCategory/pageQuery', this.params)
      this.res1 = res.data.list
      console.log(res, '查询栏目分类')
    },
    toSubmit() {
      this.$refs.myForm.validate(async(valid) => {
        if (valid) {
          // 发送保存更新请求
          const res = await saveOrUpdate(this.form)
          this.$message({
            type: 'success',
            message: res.message
          })
          this.visible = false
          this.getProductData()
        } else {
          this.$message({
            type: 'error',
            message: '请输入必传字段'
          })
        }
      })
    },
    toEdit(row) {
      this.form = { ...row }
      this.title = '修改产品信息'
      this.visible = true
      // this.form = {};
      // this.imageUrl = this.form.icon;
    },
    toAdd() {
      this.form = {}
      this.visible = true
      this.title = '添加产品信息'
      // this.imageUrl = "";
    },
    async handleClick(id) {
      await get('/productCategory/deleteById', id)
    }
  }
}
</script>
<style scoped>
</style>
